package controllers

import (
	"dmsv/aliyun"
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"strconv"
)

type AddtionalController struct {
	BaseController
}

func (this *AddtionalController) URLMapping() {
	this.Mapping("create", this.CreateTts)
}

// @Title TTS 转换
// @Description 转换tts
// @Description display
// @Param   param   body       process.SatisfactionDo     true   ""
// @Success 200     {object}   controllers.Code
// @router  /create [post]
func (this *AddtionalController) CreateTts() {

	type ttsParam struct {
		Text  string `json:"text"`
		Voice string `json:"voice"`
		Speed string `json:"speed"`
		Pitch string `json:"pitch"`
	}
	params := ttsParam{}

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &params); err != nil {

		logs.Error("json failed:%v", err)
		this.Data["json"] = ParamsError
		this.ServeJSON()
		return
	}

	fileName, cache := aliyun.TtsCache(params.Text, params.Voice, params.Speed, params.Pitch)
	if cache {
		this.Data["json"] = MakeResp(Success, fileName)
		this.ServeJSON()
		return
	}

	speed, _ := strconv.Atoi(params.Speed)
	pitch, _ := strconv.Atoi(params.Pitch)
	fileName, err := aliyun.TTSRquest(fileName, params.Text, params.Voice, speed, pitch)
	if err != nil {
		this.Data["json"] = MakeSysErrCode(err.Error())
		this.ServeJSON()
		return
	}

	this.Data["json"] = MakeResp(Success, fileName)
	this.ServeJSON()
	return
}

// @Title 监控检查
// @Description 健康检查
// @Description display
// @Success 200     {object}   controllers.Code
// @router  /healthy [get]
func (this *AddtionalController) Heathy() {

	this.Data["json"] = Success
	this.ServeJSON()
}
